/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.project.entity.DataObject;
import io.renren.modules.project.entity.DatasourceEntity;
import io.renren.modules.project.service.DatasourceService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/datasource")
public class DatasourceController extends AbstractController {
	@Autowired
	private DatasourceService datasourceService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("datasource:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = datasourceService.queryPage(params);

		return R.ok().put("page", page);
	}

	@GetMapping("/all")
	@RequiresPermissions("backend:list")
	public R listAll(@RequestParam Map<String, Object> params){
		List list = datasourceService.findAlllistInfo();

		return R.ok().put("data", list);
	}

	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		return R.ok().put("server", datasourceService.getById(id));
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/save")
	@RequiresPermissions("datasource:save")
	public R save(@RequestBody DatasourceEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);
		entity.setCreateTime(DateUtils.formatDefault(new Date()));
		datasourceService.saveEntity(entity);
		
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@PostMapping("/update")
	@RequiresPermissions("datasource:update")
	public R update(@RequestBody DatasourceEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		datasourceService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@PostMapping("/delete")
	@RequiresPermissions("datasource:delete")
	public R delete(@RequestBody Long[] delIds){
		datasourceService.deleteBatch(delIds);
		return R.ok();
	}

	@GetMapping("/datalist")
	@RequiresPermissions("backend:list")
	public R datalist(@RequestParam long datasourceId,@RequestParam long projectId){
		try{

			List list = datasourceService.findDatalistById(datasourceId,projectId);

			return R.ok().put("data", list);

		}catch (Exception e){
			e.printStackTrace();
			return R.error("获取数据失败！");
		}

	}

	@PostMapping("/findTableDatalist")
	@RequiresPermissions("backend:list")
	public R findTableDatalist(@RequestBody DataObject dob){
		try{
			PageUtils page = datasourceService.findTableDatalist(dob);

			return R.ok().put("page", page);

		}catch (Exception e){
			e.printStackTrace();
			return R.error("获取数据失败！");
		}

	}
}
